package com.pinyougou.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pinyougou.common.pojo.PageResult;
import com.pinyougou.mapper.SpecificationMapper;
import com.pinyougou.mapper.SpecificationOptionMapper;
import com.pinyougou.pojo.Specification;
import com.pinyougou.pojo.SpecificationOption;
import com.pinyougou.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @date 16:39 2019/7/9
 */
@Service //dubbo注解 ,修饰的类为服务提供者
@Transactional
public class SpecificationServiceImpl implements SpecificationService{
    @Autowired
    private SpecificationMapper specificationMapper;
    @Autowired
    private SpecificationOptionMapper specificationOptionMapper;

    /** 增加规格 */
    @Override
    public void save(Specification specification) {
        try {
            // 1.往规格表插入数据
            specificationMapper.insertSelective(specification);
            // 2.往规格选项表插入数据
            /*for (SpecificationOption specificationOption : specification.getSpecificationOptions()){
                specificationOption.setSpecId(specification.getId());
                specificationOptionMapper.insertSelective(specificationOption);
            }*/
            specificationOptionMapper.save(specification);

        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public void update(Specification specification) {
        // 1.修改规格
        specificationMapper.updateByPrimaryKeySelective(specification);
        // 2.修改规格选项
        // 2.1 先删除
        specificationOptionMapper.deleteBySpecId(specification.getId());
        // 2.2 再添加
        specificationOptionMapper.save(specification);
    }

    @Override
    public void delete(Serializable id) {

    }

    @Override
    public void deleteAll(Serializable[] ids) {
        try {
            // 1.删除规格选项表中的数据
            Example example = new Example(SpecificationOption.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andIn("specId", Arrays.asList(ids));
            specificationOptionMapper.deleteByExample(example);

            // 2.删除规格表中的数据
            example = new Example(Specification.class);
            Example.Criteria criteria1 = example.createCriteria();
            criteria1.andIn("id",Arrays.asList(ids));
            specificationMapper.deleteByExample(example);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public Specification findOne(Serializable id) {
        return null;
    }

    @Override
    public List<Specification> findAll() {
        return null;
    }

    /** 分页查询规格 */
    @Override
    public PageResult findByPage(Specification specification, int page, int rows) {
        try {
            PageInfo<Specification> pageInfo = PageHelper.startPage(page, rows).doSelectPageInfo(new ISelect() {
                @Override
                public void doSelect() {
                    specificationMapper.findAll(specification);
                }
            });
            return new PageResult(pageInfo.getPages(),pageInfo.getList());
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
    /** 根据规格id查询规格选项 */
    @Override
    public List<SpecificationOption> findSpecOptionsBySpecId(Long specId) {

        try {
            Example example = new Example(SpecificationOption.class);
            // 条件对象
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("specId",specId);
            example.orderBy("orders").asc();
            return specificationOptionMapper.selectByExample(example);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Map<String, Object>> findAllByIdAndName() {
        try{
            return specificationMapper.findAllByIdAndName();
        }catch(Exception e){
            throw new RuntimeException(e);
        }
    }


}
